package cn.lix.nio.demo.test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

public class NioClient {

    public static void main(String[] args) throws IOException {

        InetSocketAddress localhost = new InetSocketAddress("127.0.0.1", 9999);
        Scanner scanner = new Scanner(System.in);
        while(true){
            SocketChannel socketChannel = SocketChannel.open();
            // 设置非阻塞
            socketChannel.configureBlocking(false);
            if (!socketChannel.connect(localhost)) {
                while (!socketChannel.finishConnect()) {
                    System.out.println("链接需要时间，但是不会阻塞，可以去处理别的事。。。。。。");
                }
            }
            String next = scanner.next();
            ByteBuffer wrap = ByteBuffer.wrap(next.getBytes());
            socketChannel.write(wrap);
        }
    }
}
